package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteByPermission(id);
    }

    @Override
    public void insert(Role record, Long[] dis) {
        roleMapper.insert(record);

        if (dis!=null && dis.length>0){
            for (Long permissionId : dis) {
                roleMapper.insertBypermission(record.getId(),permissionId);
            }
        }

    }

    @Override
    public Role selectByPrimaryKey(Long id) {

        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {

        return roleMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Role record, Long[] ids) {
        roleMapper.updateByPrimaryKey(record);
        roleMapper.deleteByPermission(record.getId());
        if (ids!=null && ids.length>0){
            for (Long permissionId : ids) {
                roleMapper.insertBypermission(record.getId(),permissionId);
            }
        }

    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentpage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo<Role>(roles);
    }
}
